package com.idroc.coelacanth.service;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;

import com.idroc.coelacanth.config.GlobalConfigService;

/**
 * 线程池 服务
 * @author yuyoo (yuyoo4j@163.com)
 * @date 2009-12-11 上午08:32:22
 */
public class ThreadPoolService {
	
	/**
	 * 工作负荷的线程池
	 */
	private static ExecutorService workloadThreadPool = null;
	
	/**
	 * 工作负荷的调度线程池
	 */
	private static ScheduledExecutorService workloadSchduledThreadPool = null;
	
	/**
	 * 自维护负荷的线程池
	 */
	private static ExecutorService maintainThreadPool = null;
	
	/**
	 * 自维护负荷的调度线程池
	 */
	private static ScheduledExecutorService maintainSchduledThreadPool = null;
	
	private static int workloadWorkPoolSize = 50;
	 
	private static int workloadSheduledWorkPoolSize = 10;
	
	private static int maintainWorkPoolSize = 10;
	
	private static int maintainSheduledWorkPoolSize = 5;

	/**
	 * 获取 工作负荷的线程池
	 * @return -- 执行者服务(ExecutorService)
	 */
	public static ExecutorService getWorkloadThreadPool() {
	 
		if (null == workloadThreadPool) {
			ConfigService config = GlobalConfigService.getInstance();
			workloadWorkPoolSize = config.getInt("workload_threadpool_size", workloadWorkPoolSize);
			workloadThreadPool = Executors.newFixedThreadPool(workloadWorkPoolSize, new ThreadFactory(){
				
				private int id = 0;

				public Thread newThread(Runnable r) {
					
					Thread t = new Thread(r);
					t.setName("workloadThreadPool[poolsize:" + workloadWorkPoolSize 
							+ ",id->" + id++ + ":" + System.currentTimeMillis() + "]");
					return t;
				}				
			});
		}
		return workloadThreadPool;
	}
	
	/**
	 * 获取 工作负荷的调度线程池
	 * @return -- 调度执行者服务(ScheduledExecutorService)
	 */
	public static ScheduledExecutorService getWorkloadSchduledThreadPool() {
		
		if (null == workloadSchduledThreadPool) {
			ConfigService config = GlobalConfigService.getInstance();
			workloadSheduledWorkPoolSize = config.getInt("workload_schdule_threadpool_size", workloadSheduledWorkPoolSize);
			workloadSchduledThreadPool = Executors.newScheduledThreadPool(workloadSheduledWorkPoolSize, new ThreadFactory(){
				
				private int id = 0;

				public Thread newThread(Runnable r) {
					
					Thread t = new Thread(r);
					t.setName("workloadSchduledThreadPool[poolsize:" + workloadSheduledWorkPoolSize
							+ ",id->" + id++ + ":" + System.currentTimeMillis() + "]");
					return t;
				}				
			});
		}
		return workloadSchduledThreadPool;
	}
	

	/**
	 * 获取 自维护负荷的调度线程池
	 * @return -- 执行者服务(ExecutorService)
	 */
	public static ExecutorService getMaintainThreadPool() {
	 
		if (null == maintainThreadPool) {
			ConfigService config = GlobalConfigService.getInstance();
			maintainWorkPoolSize = config.getInt("maintain_threadpool_size", maintainWorkPoolSize);
			maintainThreadPool = Executors.newFixedThreadPool(maintainWorkPoolSize, new ThreadFactory(){
				
				private int id = 0;

				public Thread newThread(Runnable r) {
					
					Thread t = new Thread(r);
					t.setName("maintainThreadPool[poolsize:" + maintainWorkPoolSize
							+ ",id->" + id++ + ":" + System.currentTimeMillis() + "]");
					return t;
				}				
			});
		}
		return maintainThreadPool;
	}
	
	/**
	 * 获取 自维护负荷的调度线程池
	 * @return -- 调度执行者服务(ScheduledExecutorService)
	 */
	public static ScheduledExecutorService getMaintainSchduledThreadPool() {
		
		if (null == maintainSchduledThreadPool) {
			ConfigService config = GlobalConfigService.getInstance();
			maintainSheduledWorkPoolSize = config.getInt("maintain_schdule_threadpool_size", maintainSheduledWorkPoolSize);
			maintainSchduledThreadPool = Executors.newScheduledThreadPool(maintainSheduledWorkPoolSize, new ThreadFactory(){
				
				private int id = 0;

				public Thread newThread(Runnable r) {
					
					Thread t = new Thread(r);
					t.setName("maintainSchduledThreadPool[poolsize:" + maintainSheduledWorkPoolSize
							+ ",id->" + id++ + ":" + System.currentTimeMillis() + "]");
					return t;
				}				
			});
		}
		return maintainSchduledThreadPool;
	}
	
	/**
	 * 获取 工作负荷的线程池大小
	 * @return -- 线程池大小
	 */
	public static int getWorkloadWorkPoolSize() {
		return workloadWorkPoolSize;
	}

	/**
	 * 获取 工作负荷的调度线程池大小
	 * @return -- 线程池大小
	 */
	public static int getWorkloadSheduledWorkPoolSize() {
		return workloadSheduledWorkPoolSize;
	}

	/**
	 * 获取 工作负荷的调度线程池
	 * @return -- 线程池大小
	 */
	public static int getMaintainWorkPoolSize() {
		return maintainWorkPoolSize;
	}

	/**
	 * 获取 自维护负荷的调度线程池大小
	 * @return -- 调度线程池大小
	 */
	public static int getMaintainSheduledWorkPoolSize() {
		return maintainSheduledWorkPoolSize;
	}

	/**
	 * 关闭线程池服务
	 * @param now -- 是否立即关闭. true : ExecutorService.shutdownNow(), false: ExecutorService.shutdown()
	 */
	public static void shutdown(boolean now) {
				
		if (now) {
			if (null != workloadThreadPool) workloadThreadPool.shutdownNow();
			if (null != workloadSchduledThreadPool) workloadSchduledThreadPool.shutdownNow();
			if (null != maintainThreadPool) maintainThreadPool.shutdownNow();
			if (null != maintainSchduledThreadPool) maintainSchduledThreadPool.shutdownNow();
		} else {
			if (null != workloadThreadPool) workloadThreadPool.shutdown();
			if (null != workloadSchduledThreadPool) workloadSchduledThreadPool.shutdown();
			if (null != maintainThreadPool) maintainThreadPool.shutdown();
			if (null != maintainSchduledThreadPool) maintainSchduledThreadPool.shutdown();
		}
	}
}
